//

import UIKit

@objc protocol WP_MainViewDelegate:NSObjectProtocol {
    @objc optional  func SendPushView(index:NSInteger)
}

class WP_MainView: UIView {
    
    var blueview:UIView!
    var titleLabel:UILabel!
    
    var firstView:UIView!
    var secondview:UIView!
    weak var delegate:WP_MainViewDelegate?
    var Block:(()->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupView()
        setLabel1View()
        setupLabel2View()
    }
    
    
    func setupView() {
        
        blueview = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kRatioHeight(150)))
        blueview.backgroundColor = UIColorFromHex(0xFFE4E1)
        self.addSubview(blueview)
        
        let btn = UIButton(frame: CGRect(x: kRatioWidth(20), y: kStatusBarHeight+kRatioHeight(10), width: kRatioWidth(55), height: kRatioHeight(25)))
        btn.setTitle("Info", for: .normal)
        btn.setTitleColor(UIColor.blue, for: .normal)
        btn.addTarget(self, action: #selector(sender), for: .touchUpInside)
        blueview.addSubview(btn)
        
        
        titleLabel = UILabel(frame: CGRect(x: kScreenWidth/2-kRatioWidth(40), y: kStatusBarHeight+kRatioHeight(10), width: kRatioWidth(80), height: kRatioHeight(20)))
        titleLabel.textColor = UIColor.black
        titleLabel.font = FontPFRegular(18)
        titleLabel.text = "Preview"
        titleLabel.textAlignment = .center
        blueview.addSubview(titleLabel)
        
        // CGSizeMake(0,0)
        firstView = UIView.init(frame: CGRect(x: kRatioWidth(12), y: kRatioHeight(110), width: kScreenWidth - kRatioWidth(24), height: kRatioHeight(200)))
        firstView.backgroundColor = UIColor.white
        firstView.layer.cornerRadius = 6
        firstView.layer.shadowColor = UIColor.black.cgColor;
        firstView.layer.shadowOffset = CGSize(width: 0, height: 0);
        firstView.layer.shadowOpacity = 0.5;
        firstView.layer.cornerRadius = 7;
        firstView.layer.shadowRadius = 7;
        firstView.layer.masksToBounds = true;
        firstView.clipsToBounds = false;
        self.addSubview(firstView)
        
        secondview = UIView.init(frame: CGRect(x: kRatioWidth(12), y: firstView.frame.maxY+kRatioHeight(30), width: kScreenWidth - kRatioWidth(24), height: kRatioHeight(200)))
        secondview.backgroundColor = UIColor.white
        secondview.layer.cornerRadius = 6
        secondview.layer.shadowColor = UIColor.black.cgColor;
        secondview.layer.shadowOffset = CGSize(width: 0, height: 0);
        secondview.layer.shadowOpacity = 0.5;
        secondview.layer.cornerRadius = 7;
        secondview.layer.shadowRadius = 7;
        secondview.layer.masksToBounds = true;
        secondview.clipsToBounds = false;
        self.addSubview(secondview)
        
    }
    
    func setLabel1View()  {
        
        let array = ["Day","Week","Month"]
        let img = ["D","W","M"]
        
        //标题
        let label = UILabel.init(frame: CGRect(x: kRatioWidth(30), y: kRatioHeight(30), width: kRatioWidth(200), height: kRatioHeight(20)))
        label.textColor = UIColor.black
        label.font = FontPFMedium(18)
        label.text = "Work"
        self.firstView.addSubview(label)
        
        let height:CGFloat = self.firstView.frame.width/3
        for i in 0..<3 {
            
            let button =  UIButton.init(frame: CGRect.init(x: CGFloat(i)*height, y: kRatioHeight(60), width: self.firstView.frame.width/3, height: kRatioHeight(110)))
            button.setTitle(array[i], for: .normal)
            button.setTitleColor(.blue, for: .normal)
            button.setImage(UIImage.init(named: img[i]), for: .normal)
            button.titleLabel?.font = FontPFRegular(18)
            button.tag = 1+i
            button.titleEdgeInsets = UIEdgeInsets(top: (button.imageView!.frame.size.height)+kRatioHeight(10), left: -(button.imageView!.frame.size.width), bottom: -(button.imageView!.frame.size.height)-(kRatioHeight(10)/2), right: 0)
            button.imageEdgeInsets = UIEdgeInsets(top: -(button.titleLabel!.intrinsicContentSize.height)-(kRatioHeight(10)/2), left: 0, bottom: 0, right: -(button.titleLabel!.bounds.size.width))
            
            button.addTarget(self, action: #selector(click(sender:)), for: .touchUpInside)
            self.firstView.addSubview(button)
        }
        
    }
    
    func setupLabel2View() {
        //标题
        let label = UILabel.init(frame: CGRect(x: kRatioWidth(30), y: kRatioHeight(20), width: kRatioWidth(200), height: kRatioHeight(20)))
        label.textColor = UIColor.black
        label.font = FontPFMedium(18)
        label.text = "The Contact"
        self.secondview.addSubview(label)
        
        let img = UIImageView.init(frame: CGRect(x:kRatioWidth(40) , y: kRatioHeight(80), width: kRatioWidth(40), height: kRatioHeight(40)))
        img.image = UIImage.init(named: "header")
        img.isUserInteractionEnabled = true
        self.secondview.addSubview(img)
        
        let label2 = UILabel.init(frame: CGRect(x: kRatioWidth(5), y: img.frame.maxY, width: self.firstView.frame.width/3, height: kRatioHeight(50)))
        label2.textColor = UIColor.blue
        label2.font = FontPFRegular(18)
        label2.textAlignment = .center
        label2.text = "Customer"
        label2.isUserInteractionEnabled = true
        self.secondview.addSubview(label2)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(ddd(sender:)))
        img.addGestureRecognizer(tap)
        let tap2 = UITapGestureRecognizer.init(target: self, action: #selector(ddd(sender:)))
        label2.addGestureRecognizer(tap2)
        
    }
    
    //button事件
    @objc func click(sender:UIButton)  {
       
        delegate?.SendPushView!(index: sender.tag)
        
    }
    //label事件
    @objc func ddd(sender:UITapGestureRecognizer) {
        print("bchfdsddgg")
        delegate?.SendPushView!(index: 4)
    }
    //信息中心
    @objc func sender() {
        Block!()
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
}
